package Facade;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import Entity.Contract;
import Entity.Offer;

/**
 * Session Bean implementation class ContratFacade
 */
@Stateless
public class ContractFacade implements ContractFacadeRemote {
	@PersistenceContext(unitName = "POLEINFODB")
	protected EntityManager entityMgr;
	
    /**
     * Default constructor. 
     */
    public ContractFacade() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Contract addContract(Contract c) {
		entityMgr.persist(c);
		return c;
	}

	@Override
	public void clearAllContracts() {
		Collection<Contract> Contracts = listContracts();
		for(Contract nextContract : Contracts){
			entityMgr.remove(nextContract);
		}
	}

	@Override
	public void clearContract(int id_Contract) {
		Contract c =entityMgr.find(Contract.class, id_Contract);
		entityMgr.remove(c);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Contract> listContracts() {
		return entityMgr.createQuery("SELECT c FROM Contract as c").getResultList();
	}



}
